package com.wwu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwu.common.constant.CommConst;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.web.ResultObj;
import com.wwu.system.common.utils.TreeUtils;
import com.wwu.system.entity.SysMenuRole;
import com.wwu.system.mapper.SysMenuRoleMapper;
import com.wwu.system.service.ISysMenuRoleService;
import com.wwu.system.vo.TreeVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Arrays.asList;

/**
 *  系统菜单角色服务实现类
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Service
public class SysMenuRoleServiceImpl extends ServiceImpl<SysMenuRoleMapper, SysMenuRole> implements ISysMenuRoleService {
    @Resource
    private SysMenuRoleMapper sysMenuRoleMapper;

    /**
     * 根据角色id查询菜单树
     * @param roleId 角色id
     * @return com.wwu.common.web.ResultObj
     * @author 一蓑烟雨
     * @date 2023/8/16 19:42
     */
    @Override
    public ResultObj getSysMenuIdsByRoleId(String roleId) {
        List<TreeVO> menuList = sysMenuRoleMapper.getSysMenuIdsByRoleId(roleId);
        List<TreeVO> parentMenuList = new ArrayList<>();
        List<TreeVO> childMenuList = new ArrayList<>();

        if(ObjectUtils.isEmpty(menuList)){
            return ResultObj.success();
        }

        //取出模块根节点
        for(TreeVO treeVO: menuList){
            if(treeVO.getType().equals(0)){
                parentMenuList.add(treeVO);
            }
        }

        for(TreeVO treeVO: parentMenuList){
            childMenuList = TreeUtils.getChildTree(treeVO.getId(),menuList);
            treeVO.setChildren(childMenuList);
        }
        return ResultObj.success(parentMenuList);
    }

    /**
     * 根据菜单ID删除对应的角色菜单
     * @param delMenuIds   菜单ID集合
     * @return boolean
     * @author 一蓑烟雨
     * @date 2023/7/6 10:51
     */
    @Override
    public boolean deleteMenuRoleByMenuIds(List<String> menuIds) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(menuIds),"菜单Id不能为空");
        LambdaQueryWrapper<SysMenuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysMenuRole::getMenuId,menuIds);

        //如果数量大于0则执行删除操作
        if(this.count(wrapper) > 0){
            AssertUtil.isTrue(!this.remove(wrapper),"菜单角色删除失败");
        }
        return true;
    }

    /**
     * 根据角色id更新权限
     * @param sysMenuRole  菜单角色实体
     * @author 一蓑烟雨
     * @date 2023/8/17 16:07
     */
    @Override
    public void updateMenuRole(SysMenuRole sysMenuRole) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(sysMenuRole.getRoleId()),"角色Id不能为空");
        AssertUtil.isTrue(ObjectUtils.isEmpty(sysMenuRole.getMenuId()),"角色权限不能为空");

        LambdaQueryWrapper<SysMenuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysMenuRole::getRoleId,sysMenuRole.getRoleId());
        if(this.count(wrapper)>0){
            //先删除角色权限
            this.remove(wrapper);
        }
        List<String> menuIds = Arrays.asList(sysMenuRole.getMenuId().split(CommConst.SPLIT_COMMA));
        List<SysMenuRole> sysMenuRoleList =  new ArrayList<>();
        SysMenuRole sysMenuRoleTemp = null;
        for(String menuId: menuIds){
            sysMenuRoleTemp =  new SysMenuRole();
            sysMenuRoleTemp.setRoleId(sysMenuRole.getRoleId());
            sysMenuRoleTemp.setMenuId(menuId);

            sysMenuRoleList.add(sysMenuRoleTemp);
        }
        //批量插入
        AssertUtil.isTrue(!this.saveBatch(sysMenuRoleList),"角色权限更新失败");
    }
}
